package {
	import Classes.CLoader;
	
	import UIFree.uif_Timer;
	import UIFree.uif_component;
	
	import cardsbody.*;
	
	import editor.pi_cardbody;
	import editor.pi_maskInfo;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.system.Capabilities;
	import flash.system.Security;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	
	[SWF(width="760", height="730", backgroundColor="#FFFFFF")]
	public class pj extends pi_app {
		
		private var _fullView		: pi_cardbody;
		private var _photo			: CLoader;
		private var _majorVersion	: Number;
		private var _link			: uif_component;
		
		private const APPWIDTH: int = 760;
		private const APPHEIGHT: int = 730;
		
		public function get majorVersion(): Number {
			return _majorVersion;
		}
		
		public function get photo(): CLoader {
			return _photo;
		}

		override public function getInner(incx: Number=0, incy: Number=0, incw:Number=0, inch:Number=0):Rectangle {
			return new Rectangle(0, 0, APPWIDTH, APPHEIGHT);
		}
		
		public function get isPhotoLoaded(): Boolean {
			return (_photo != null) && (_photo.contentLoaderInfo.bytesLoaded > 0);
		}
		
		public function get isFullView(): Boolean {
			return (_fullView != null) && _fullView.visible;
		}
		
		public function get fullScreen(): pi_cardbody {
			return _fullView;
		}
		
		public function pj() {
			Security.allowDomain('*');
			super();
			resize(APPWIDTH, APPHEIGHT);
			
			var pack: Object = root.loaderInfo.parameters;
			if (!pack['cardClass']) {
//id=25&url=data/mistic.swf&class=[class pi_pjc]&colors=1,1,1,1,0,0,0,0&pos=0,0,80,80&gray=0&matrix=0.29572216425696823,0,0,0.29572216425696823,-32.976598598897,-9.017737367715002&img=data/i-36.jpg&cardClass=pi_pjc
				pack = {
					id: 25,
					cardClass: 'pi_pjc',
					url: 'http://4607.ovz9.hc.ru/games/data/images/cards/mistic.swf',
					colors: '1,1,1,1,0,0,0,0',
					pos: '0,0,80,80',
					matrix: '0.29572216425696823,0,0,0.29572216425696823,-32.976598598897,-9.017737367715002',
					img: 'http://content.foto.my.mail.ru/mail/fwadim/29/i-36.jpg'
				}
			}
//			initReceiveView(root.loaderInfo.parameters['state'], root.loaderInfo.parameters['card'])			
			initReceiveView(pack)			
		}
		
		public function checkVersion(maxVersion: int): Boolean {
			import flash.system.Capabilities;
			var versionNumber:String = Capabilities.version;
			var versionArray:Array = versionNumber.split(",");
			var length:Number = versionArray.length;
			var platformAndVersion:Array = versionArray[0].split(" ");
			_majorVersion = parseInt(platformAndVersion[1]);
			
			return _majorVersion > 8;
		}
		
		protected function toMaskInfo(data: Object, matrix: Matrix=null): pi_maskInfo {
			return new pi_maskInfo(data.id, data.url, data.maskClass,
								new Rectangle(data.pos[0], data.pos[1], data.pos[2], data.pos[3]),
								new ColorTransform(data.colors[0], data.colors[1], data.colors[2], data.colors[3], 
													data.colors[4], data.colors[5], data.colors[6]),
								matrix, data.price, data.extend);
		}

		public function fullView(matrix: Matrix, cardInfo: Object =null, a_photo: CLoader=null): void {
			if (cardInfo != null) {
				var mi: pi_maskInfo = toMaskInfo(cardInfo, matrix);

				var ir: Rectangle = new Rectangle(0, 0, APPWIDTH, APPHEIGHT);
				var cardClass: Class;
				switch (cardInfo['cardClass']) {
					case 'pi_pngcard'	: cardClass = pi_pngcard;
											break;
					case 'pi_pjc'		: cardClass = pi_pjc;
											break;
					case 'pi_pngcardswf': cardClass = pi_pngcard;
											break;
				}
				_fullView = new cardClass(this, ir.x, ir.y, ir.width, ir.height, 'fullView');
				_fullView.visible = false;
				a_photo = a_photo?a_photo:_photo;
				
				a_photo.duplicate(_fullView.card_photo);
				_fullView.card_photo.visible = true;
				_fullView.setMaskInfo(mi);
				_fullView.slideShow();
				
/*				
				_link = new uif_component(this, APPWIDTH - 80, APPHEIGHT - 58, 80, 58, 'button');
				_link.visible = false;
				_link.buttonMode = true;
				with (_link.addChild(new c_link())) {
					x = 10,
					y = 10
				};
				
				new uif_Timer(2000, null, function(e: Event): void {
					_link.fx('x', APPWIDTH - 80, _fullView.card_mask.source.x + 
								(_fullView.card_mask.source.width - _link.width) / 2, 
							null, 50, 1000).start();
					_link.slideShow();
				}, 1);
				
				_link.addEventListener(MouseEvent.CLICK, function(e: MouseEvent): void {
					navigateToURL(new URLRequest('http://my.mail.ru/cgi-bin/my/app-canvas?appid=441805'), '_blank');
				});*/
			}
		}
		
		public function externalData(data: *): void {
			dispatchEvent(new GDataEvent(GDataEvent.EXTERNALDATA, data));
		}
		
		public function initReceiveView(pack: Object): void {
			_photo = new CLoader(this, pack.img, function(e: Event): void {
				var matA: Array = pack.matrix.split(',');
				var matrix: Matrix = new Matrix(matA[0], matA[1], matA[2], matA[3], matA[4], matA[5]);
				
				pack.pos 	= pack.pos.split(','); 
				pack.colors = pack.colors.split(',');
				pack.url 	= pack.url;
								
				fullView(matrix, pack, _photo);
			});
			_photo.visible = false;
		}		
	}
}
